package com.lxc.demos.android.handler;

import com.lxc.demos.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HandlerDemo extends Activity {
	private static final String TAG = "HANDLERDEMO"; 
	
	TextView thread_info;
	TextView msg_info;
	Button handlerThread_handler;

	
	MyHandler myHandler1;   //通HandlerThread中的Looper构造
	MyHandler myHandler2;    //直接
	HandlerThread handlerThread;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.handlerdemo);
		thread_info = (TextView) findViewById(R.id.thread_info);
		msg_info = (TextView) findViewById(R.id.msg_info);
		handlerThread_handler = (Button)findViewById(R.id.handlerThread_handler); 
		
		//记录当前线程的信息（ID and name）
		String info = "Activity--> ID = "+Thread.currentThread().getId()+" name = "+Thread.currentThread().getName();
		thread_info.setText(info);
		Log.d(TAG,info );
		
		//生成一个HandlerThread对象，实现了使用Looper来处理消息队列的功能，
		//这个类由Android应用程序框架提供。
		handlerThread = new HandlerThread("handler_thread");
		//在使用HandlerThread的getLooper()方法之前，必须调用该类的start();
		handlerThread.start();
		
		
	}
	
	/**
	 * 	生成一个HandlerThread对象，实现了使用Looper来处理消息队列的功能，
	 *  这个类由Android应用程序框架提供。
	 *  @param view
	 */
	public void handlerThread_handler(View view){

		String info = "Method_Thread--> ID = "+Thread.currentThread().getId()+" name = "+Thread.currentThread().getName();
		Log.d(TAG,info );
		
		myHandler1 = new MyHandler(handlerThread.getLooper());
		
		//将msg发送到目标对象，所谓的目标对象，就是生成该msg对象handler
		Message msg = myHandler1.obtainMessage();
		
		Bundle bundle = new Bundle();
		bundle.putString("name", "lxc");
		bundle.putInt("age", 20);
		msg.setData(bundle);
		
		msg.obj = info;
		
		msg.sendToTarget();
	}
	
	public void handlerDefault_handler(View view){
		
		String info = "Method_Default--> ID = "+Thread.currentThread().getId()+" name = "+Thread.currentThread().getName();
		Log.d(TAG,info );
		
		myHandler2 = new MyHandler();
		
		//将msg发送到目标对象，所谓的目标对象，就是生成该msg对象handler
		Message msg = myHandler2.obtainMessage();
		
		Bundle bundle = new Bundle();
		bundle.putString("name", "lxc");
		bundle.putInt("age", 20);
		msg.setData(bundle);
		
		msg.obj = info;
		
		msg.sendToTarget();
	}
	
	class MyHandler extends Handler{

		public MyHandler() {
			super();
			// TODO Auto-generated constructor stub
		}

		public MyHandler(Looper looper) {
			super(looper);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			Bundle bundle = msg.getData();
			int age = bundle.getInt("age");
			String name = bundle.getString("name");
			String info = "Handler--> ID = "+Thread.currentThread().getId()+" name = "+Thread.currentThread().getName();
			thread_info.setText(info);
			Log.d(TAG, info);
			Log.d(TAG, "age is "+ age + ", name is "+name);
			Log.d(TAG, "handlerMessage");
			
			String msgInfo = (String) msg.obj;
			msg_info.setText(msgInfo);
		
		}

		@Override
		public void dispatchMessage(Message msg) {
			// TODO Auto-generated method stub
			super.dispatchMessage(msg);
		}

		@Override
		public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
			// TODO Auto-generated method stub
			return super.sendMessageAtTime(msg, uptimeMillis);
		}
		
	}

}
